//
//  BIDMyMessageListViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/17.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDMyMessageListViewController.h"
#import "NSDate+Category.h"
#import "BIDMyMessageCell.h"

static NSString *messageListURL = @"messageAndroid/list.action";

static NSString *deleteURL = @"messageAndroid/remove.action";

@interface BIDMyMessageListViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    BOOL _isRegister;
    BIDMyMessageCell *_tempCell;
}
@end

@implementation BIDMyMessageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的消息";
    _tempCell = [[[NSBundle mainBundle] loadNibNamed:@"BIDMyMessageCell" owner:self options:nil] lastObject];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], messageListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"memberId=%@", [AppDelegate getUserId]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
        }
        [self.myTableView reloadData];
        [self autoDecideToShowEmptyView];
    }];
}

- (void)deleteMessageAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    NSDictionary *dic = self.dataSourceArr[row];
    NSString *strMessageID = dic[@"id"];
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], deleteURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"ids=%@", strMessageID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary* responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            //删除成功
            [self.dataSourceArr removeObjectAtIndex:row];
            [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_isRegister)
    {
        _isRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDMyMessageCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDMyMessageCell *messageCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = self.dataSourceArr[row];
    messageCell.titleLabel.text = dic[@"message"];
    NSString *strTime = dic[@"createTime"];
    NSDate *date = [BIDGlobalMethods transToNSDateByFormatter:@"yyyy-MM-dd HH-mm-ss" fromStringDate:strTime];
    messageCell.timeLabel.text = [date minuteDescription];
    cell = messageCell;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0.f;
    NSUInteger row = indexPath.row;
    NSDictionary *dic = self.dataSourceArr[row];
    _tempCell.titleLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.frame)-24;
    _tempCell.titleLabel.text = dic[@"message"];
    CGSize size = [_tempCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    height = size.height<65 ? 65 : size.height+1;
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //删除某条通知
        [self deleteMessageAtIndexPath:indexPath];
    }
}

@end
